package com.lagou.client.handler;

import com.lagou.service.pojo.RpcRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;

/**
 * @author Administrator
 * @version 1.0
 * @date 2020/7/4 9:05
 * 自定义事件处理器
 */
public class UserClientHandler extends ChannelInboundHandlerAdapter implements Callable {

    private ChannelHandlerContext channelHandlerContext;//事件处理器上下文对象
    private String result;//记录服务器返回的数据
    private RpcRequest param;//记录要返回给服务端的数据

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        this.channelHandlerContext=ctx;
    }

    @Override
    public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //将读到的服务器数据设置为成员变量
        System.out.println("==客户端获取到数据：=="+msg.toString());
        result = msg.toString();
        notify();
    }

    public synchronized Object call() throws Exception {
        channelHandlerContext.writeAndFlush(param);
        wait();
        return result;
    }
    //生成代理对象的时候封装好的参数信息
    public void setParam(RpcRequest param){
        this.param = param;
    }


}
